﻿using System.IO;
using System.Text;
using Best.SmartCms.CodeGeneration.Model;
using Best.SmartCms.CodeGeneration.Template;

namespace Best.SmartCms.CodeGeneration.BuiderFactory
{
    public class JsBuilder : CodeGenerationFactory
    {
        public override void BuilderTemplate(CodeGenerationDto dto)
        {
            if (!dto.IsBulderJs)
            {
                return;
            }

            var path = Path.Combine(BasePath, $"Best.SmartCms.Admin/ClientApp/src/app/routes/{dto.DomainName.ToLower()}");
            var dtopath = Path.Combine(BasePath, $"Best.SmartCms.Admin/ClientApp/src/app/shared/model");
           
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            if (!Directory.Exists(dtopath))
            {
                Directory.CreateDirectory(dtopath);
            }

            var listjs = Path.Combine(path, $"{dto.DomainName.ToLower()}.component.ts");
            var listhtml = Path.Combine(path, $"{dto.DomainName.ToLower()}.component.html");
            var createjs = Path.Combine(path, $"create-{dto.DomainName.ToLower()}.component.ts");
            var createhtml = Path.Combine(path, $"create-{dto.DomainName.ToLower()}.component.html");
            var editjs = Path.Combine(path, $"edit-{dto.DomainName.ToLower()}.component.ts");
            var edithtml = Path.Combine(path, $"edit-{dto.DomainName.ToLower()}.component.html");
            var jsdto = Path.Combine(dtopath, $"{dto.DomainName.ToLower()}dto.ts");

            var dao = new JsDtoTempate(dto);
            var daoContent = dao.TransformText();
            File.WriteAllText(jsdto, daoContent, Encoding.UTF8);

            var jsList = new JsListTempate(dto);
            var jsListContent = jsList.TransformText();
            File.WriteAllText(listjs, jsListContent, Encoding.UTF8);

            var htmlList = new HtmlListTempate(dto);
            var htmlListContent = htmlList.TransformText();
            File.WriteAllText(listhtml, htmlListContent, Encoding.UTF8);

            var jsCreate = new JsCreateTempate(dto);
            var jsCreateContent = jsCreate.TransformText();
            File.WriteAllText(createjs, jsCreateContent, Encoding.UTF8);

            var htmlCreate = new HtmlCreateTempate(dto);
            var htmlCreateContent = htmlCreate.TransformText();
            File.WriteAllText(createhtml, htmlCreateContent, Encoding.UTF8);

            var jsEdit = new JsEditTempate(dto);
            var jsEditContent = jsEdit.TransformText();
            File.WriteAllText(editjs, jsEditContent, Encoding.UTF8);

            var htmlEdit = new HtmlEditTempate(dto);
            var htmlEditContent = htmlEdit.TransformText();
            File.WriteAllText(edithtml, htmlEditContent, Encoding.UTF8);

        }
    }
}
